package com.ggkt.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeStampUtil {

    /**
     * 时间戳字符串转换日期字符串
     */
    public static String stampToDateStr(String timeStamp) {
        if (timeStamp.length() == 10) {
            timeStamp = timeStamp + "000";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 时间戳转换日期
        String date = sdf.format(new Date(Long.parseLong(timeStamp)));
        return date;
    }

    /**
     * 日期字符串转化为时间戳
     */
    public static long dateToStamp(String dateTime) {
        Date date = null;
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timeStemp = timeStemp = date.getTime();
        String timeString = String.valueOf(timeStemp).substring(0, 10);
        return Long.parseLong(timeString);
    }

    /**
     * 日期字符串转化为时间戳
     */
    public static long dateToStampOriginal(String dateTime) {
        Date date = null;
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 日期字符串转换日期
     */
    public static Date strToDate(String timeStamp) {
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = formatter.parse(timeStamp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 日期转换日期字符串
     */
    public static String dateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

}
